package com.xgz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xgz.dao.TUserDao;
import com.xgz.entity.MyPage;
import com.xgz.entity.TUser;
import com.xgz.service.TUserService;
import com.xgz.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class TUserServiceImpl implements TUserService {

    /**
     * 服务对象
     */
    @Autowired
    private TUserDao tUserDao;

    /**
     * 添加用户
     * @param tUser
     * @return
     */
    @Override
    public boolean addUser(TUser tUser) {
        int insert=-1;
        if(tUser.getId()==null) {
            //创建时间
            tUser.setCreatetime(new Date());
            // 获取盐值
            String salt = UUID.randomUUID().toString();
            //加密密码
            String oldpwd = tUser.getPassword() + salt;
            String newpassword = DigestUtils.md5DigestAsHex(oldpwd.getBytes());
            tUser.setSalt(salt);
            tUser.setPassword(newpassword);
            insert = tUserDao.insert(tUser);
        }else{
            insert = tUserDao.updateByPrimaryKey(tUser);
        }
        return insert>=0?true:false;
    }

    /**
     * 登录
     * @param tUser
     * @return
     */
    @Override
    public TUser login(TUser tUser) {
        //查询数据库里是否有数据
        List<TUser> login = tUserDao.login(tUser.getUsername());
        if (login.size()!=0){
            //获取输入的密码
            String password = tUser.getPassword();
            //获取数据库里的盐值
            String salt = login.get(0).getSalt();
            //将获取输入的密码进行加密    MD5Util加密方式
            String loginpw = MD5Util.MD5EncodeUtf8(tUser.getPassword(), salt);
            //自带的加密方式
            String s = DigestUtils.md5DigestAsHex((password + salt).getBytes());
            if (loginpw.equals(login.get(0).getPassword()) || s.equals(login.get(0).getPassword()) || password.equals(login.get(0).getPassword())){
                return tUser;
            }
        }
        return null;
    }

    /**
     * 用户名失焦判断
     * @param username 筛选条件
     * @return
     */
    @Override
    public boolean queryByName(String username) {
/*        TUser tUser = new TUser();
        tUser.setUsername(username);
        TUser user = tUserDao.selectOne(tUser);*/
        List<TUser> login = tUserDao.login(username);
        if (login.size() !=0){
            return true;
        }
        return false;
    }

    /**
     * 查询所有数据
     * @param myPage
     * @param tUser
     * @return
     */
    @Override
    public PageInfo queryAll(MyPage myPage, TUser tUser) {
        PageHelper.startPage(myPage.getPage(), myPage.getLimit());
        PageInfo pageInfo = new PageInfo(tUserDao.selectAll());
        return pageInfo;
    }

    /**
     * 修改用户
     * @param tUser
     * @return
     */
    @Override
    public boolean updUser(TUser tUser) {
        //根据id修改状态
        int i = tUserDao.updateByPrimaryKeySelective(tUser);
        return i>0?true:false;
    }

    /**
     * 模糊查询
     * @param tUser
     * @return
     */
    @Override
    public PageInfo query(TUser tUser) {
        List<Map> query = tUserDao.query(tUser);
        System.out.println("query = " + query);
        System.out.println("query = " + query.size());
        return new PageInfo(query);
    }


}
